package com.niodata.dp.task.dao;

import com.niodata.dp.task.entity.DynamicExecution;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface DynamicExecutionMapper {

  @Insert("insert into DYNAMIC_EXECUTION(EXEC_NAME,ID,OWNER_ID,OWNER_TYPE,TASK_NAME,"
      + "START_TIME,PARAMS,DESCRIPTION,STATUS,IS_USER_CREATE,TYPE)"
      + "VALUES(#{exe.name},#{exe.id},#{exe.ownerId},#{exe.ownerType},#{exe.taskName},"
      + "#{exe.startTime},#{exe.params},#{exe.description},#{exe.status},#{exe.userCreate},"
      + "#{exe.type})")
  public void save(@Param("exe") DynamicExecution exe);

  @Select("select * from DYNAMIC_EXECUTION WHERE ID=#{id}")
  @ResultMap("dynamicExecResultMap")
  public DynamicExecution getById(String id);

  public List<DynamicExecution> getUserAuthorizedDynamicExecByDays(
      @Param("userId") long userId,
      @Param("days") int days,
      @Param("offset") int offset,
      @Param("count") int count,
      @Param("keyWord") String keyWord,
      @Param("type") String type);

  @Delete("delete from DYNAMIC_EXECUTION where ID=#{id}")
  public void delete(@Param("id") String id);

  @Update("update DYNAMIC_EXECUTION set EXEC_ID=#{execId} where ID=#{id}")
  public void updateExecId(@Param("id") String id, @Param("execId") int execId);

  @Select("select * from DYNAMIC_EXECUTION where STATUS = 'RUNNING'")
  @ResultMap("dynamicExecResultMap")
  public List<DynamicExecution> getRunningDynamicExecution();


  @Update("update DYNAMIC_EXECUTION set END_TIME=#{endTime}, STATUS=#{status} where ID=#{id}")
  public void updateExecStatusAndTime(@Param("id") String id, @Param("endTime") Date endTime,
      @Param("status") String status);
}
